package com.bangdao.hu;

import static com.bangdao.hu.roomba.Views.DriveView.REQUEST_ENABLE_BT;
import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bangdao.hu.roomba.ConnectedThread;
import com.bangdao.hu.roomba.RoutePlan.RoutePlanner;
import com.bangdao.hu.roomba.Views.DriveView;
import com.bangdao.hu.roomba.Views.MapGenerationView;
import com.bangdao.hu.roomba.Views.TestDriveView;
import com.bangdao.hu.roomba.WifiSignalStrengthMap.MapGenerator;
public class TestBluetoothActivity extends Activity {
	public static ConnectedThread btThread;	//only one thread will be exist
	private final String ROOMBA_MAC_ADDR = "00:0A:3A:2E:CA:1D";
	private final String ROOMAB_BT_NAME = "Element Serial";
	private BluetoothAdapter mBluetoothAdapter;
	private TextView pairInfo;
	private Button connectButton;
	private Button disconnectButton;
	private BluetoothSocket socket;
	static public WifiManager wifiManager;
	static public AssetManager assetManager;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		assetManager = getAssets();
    }
        
    private void setupViews() {
		pairInfo = (TextView)findViewById(R.id.pair_text);
		connectButton = (Button)findViewById(R.id.connect_button);
		disconnectButton = (Button)findViewById(R.id.disconnect_button);
		disconnectButton.setVisibility(View.GONE);	//hide disconnect button
		
		pairInfo.setText("Roomba bluetooth device: \n" + ROOMAB_BT_NAME + " " + ROOMBA_MAC_ADDR);		
	
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    // Device does not support Bluetooth
		}		
		if (!mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		
		connectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
//				int[] goal = RoutePlanner.getInstance().search();
//				if(goal == null)
//					System.out.println("not found");
//				else
//					printArray(goal);
//				RoutePlanner.getInstance().testSmooth();
//					System.out.println(goal[0] + " " + goal[1] + " " + goal[2]);
//				startActivity(new Intent(TestBluetoothActivity.this, MapGenerationView.class));
				connect();
//				Log.i("StringParsing", MapGenerator.getInstance().parseMapFile().toString());
			}
		});
		
		disconnectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				disconnect();
			}
		});	
	}
    
    private void printArray(int[] array)
    {
    	for(int i = 0; i < array.length; i++)
    	{
    		System.out.print(array[i] + " ");
    	}
    	System.out.println();	
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }
	
	private void connect()
	{
		BluetoothDevice irobot = mBluetoothAdapter.getRemoteDevice(ROOMBA_MAC_ADDR);
		if(irobot == null)
		{
			Toast.makeText(getApplicationContext(), "check the device is on and try it again", Toast.LENGTH_LONG).show();
			return;
		}
		
		 try {
                 socket = irobot.createRfcommSocketToServiceRecord(UUID
                         .fromString("00001101-0000-1000-8000-00805f9b34fb"));
                 socket.connect();
                 btThread = new ConnectedThread(socket);
                 btThread.startup();
                 btThread.pause(100);
                 btThread.control();
                 Toast.makeText(TestBluetoothActivity.this, "connected", Toast.LENGTH_LONG).show();
                 disconnectButton.setVisibility(View.VISIBLE);	//disonncectButton revealed
                 connectButton.setVisibility(View.GONE);		//hide connect button
//                 startActivity(new Intent(TestBluetoothActivity.this, DriveView.class));
                 startActivity(new Intent(TestBluetoothActivity.this, TestDriveView.class));
                 return;
			}catch (IOException e) {
                Toast.makeText(TestBluetoothActivity.this, " unable to connecting to " + ROOMBA_MAC_ADDR, Toast.LENGTH_LONG).show();     
                return;
			}             
	}
	
	private void disconnect()
	{
		try {
			btThread.setLEDs(false, false, false, false, false, false, 0, 255);	//turn power color back to green
			socket.close();
			disconnectButton.setVisibility(View.GONE);
			connectButton.setVisibility(View.VISIBLE);
			Toast.makeText(TestBluetoothActivity.this, "disconnected", Toast.LENGTH_LONG).show();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}